package com.poten.utils;

import com.github.wxpay.sdk.WXPayUtil;
import com.poten.pojo.TbPayLog;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class WechatPayUtils {

    public static Map<String, String> callWechatPay(TbPayLog payLog) {

        String orderId = UUID.randomUUID().toString().substring(10);

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appid", "wxab8acb865bb1637e");
        paramMap.put("mch_id", "11473623");
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
        paramMap.put("body", payLog.getPid());
        paramMap.put("out_trade_no", payLog.getPid());
        paramMap.put("total_fee", "1");
        paramMap.put("spbill_create_ip", "120.78.167.80");
        paramMap.put("notify_url", "http://www.lizi.cn/wchatPay/notify");
        paramMap.put("trade_type", "NATIVE");

        try {
            String generateSignedXml = WXPayUtil.generateSignedXml(paramMap, "2ab9071b06b9f739b950ddb41db2690d");
            System.out.println(generateSignedXml);

            String  url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            HttpClient client = new HttpClient(url);
            client.setHttps(true);
            client.setXmlParam(generateSignedXml);
            client.post();

            //3.获取结果
            String xmlResult = client.getContent();

            Map<String, String> xmlToMap = WXPayUtil.xmlToMap(xmlResult);
            return xmlToMap;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    public static Map<String,String> getWechatNotifyData(HttpServletRequest request){

        //支付成功后，这个方法会接受到一条消息，消息是流形式
        InputStream inStream = null;
        try {
            inStream = request.getInputStream();
            //把消息流转换成文本类型
            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inStream.read(buffer)) != -1) {
                outSteam.write(buffer, 0, len);
            }
            outSteam.close();
            String result = new String(outSteam.toByteArray(), "UTF-8");
            System.out.println("result====="+result);
            Map<String, String> map = WXPayUtil.xmlToMap(result.toString());
            System.out.println("map====="+map);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String returnError(){
        return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[error]]></return_msg></xml>";
    }

    public static String returnSuccess(){
        return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }


}
